<?php

//index -> новини
class IndexController extends Zend_Controller_Action
{

    public function init()
    {
    }

    public function indexAction()
    {
        $this->view->headLink()->appendStylesheet('/stylesheets/news.css');
        $this->view->headLink()->appendStylesheet('/frameworks/fancybox/jquery.fancybox-1.3.4.css');
        $this->view->headScript()->appendFile($this->view->baseUrl('/frameworks/jquery/js/jquery-1.7.2.min.js'));
        $this->view->headScript()->appendFile($this->view->baseUrl('/frameworks/fancybox/jquery.fancybox-1.3.4.pack.js'));
        $this->view->headScript()->appendFile($this->view->baseUrl('/frameworks/fancybox/jquery.easing-1.3.pack.js'));
        $this->view->headScript()->appendFile($this->view->baseUrl('/frameworks/fancybox/jquery.mousewheel-3.0.4.pack.js'));
        $this->view->headScript()->appendFile($this->view->baseUrl('/js/start_fansy_box.js'));
        
        $newsService = Application_Model_NewsService::getInstance();
        $newsPaginator = $newsService->getPaginatorRows();
        $pageId = (int) $this->getRequest()->getParam('page');
        if (isset($pageId))
        {
            $newsPaginator->setCurrentPageNumber($pageId);
        }
        
        $this->view->paginator = $newsPaginator;
        $this->view->news = $newsPaginator->getCurrentItems();
        $pictureNewsService = new Application_Model_PicturesNewService();
        foreach ($this->view->news as $key => $new){
            $this->view->news[$key]['pictures_new'] = $pictureNewsService-> findByNewId($new['id']);
        }
        $reg = Zend_Registry::getInstance();
        $this->view->role = $reg->currentUser->get('role');
    }

    public function addAction()
    {
        $registry = Zend_Registry::getInstance();
        if (!$registry->acl->isAllowed($registry->currentUser->get('role'), 'add_news')){
            $this->_redirect(   $this
                            ->view
                            ->url(array('controller' => 'office',
                                        'action' => 'index')));
            return;
        }
        
        $this->_helper->layout->setLayout('layout_work');
        
        $formAddNews = new Application_Form_New();
        if ($this->_request->isPost())
        {
            $formData = $this->_request->getPost();
            if ($formAddNews->isValid($formData))
            {
                $formAddNews->processUploadedFiles();
                $values = $formAddNews->getValues();
                $news = new Application_Model_New();
                $news->set('title', $values['title']);
                $news->set('description', $values['description']);
                $news->set('date_create', time());
                $newsService = Application_Model_NewsService::getInstance();
                $newId = $newsService->save($news);
                
                $savePath = Application_Model_PathsService::getInstance()->get("news");
                if ($savePath != null && isset($values['photos'])){
                    rename(
                        Application_Model_PathsService::getInstance()->get("temp") . $values['photos'], 
                        $savePath . $values['photos']
                    );
                    
                    $picture_new = new Application_Model_PictureNew();
                    $picture_new -> set('file_name', $values['photos']);
                    $picture_new -> set('new_id', $newId);
                    $pictureNewService = new Application_Model_PicturesNewService();
                    $pictureNewService -> save($picture_new);
                }
                
                $this->_redirect(   $this
                                    ->view
                                    ->url(array('controller' => 'index',
                                                'action' => 'index')));
                return;
            }
        }
        $this->view->formAddNews = $formAddNews;
    }

    public function delAction()
    {
        $registry = Zend_Registry::getInstance();
        if (!$registry->acl->isAllowed($registry->currentUser->get('role'), 'remove_news')){
            $this->_redirect(   $this
                            ->view
                            ->url(array('controller' => 'office',
                                        'action' => 'index')));
            return;
        }
        $delId = (int) $this->getRequest()->getParam('id');
        if (isset($delId))
        {
            $newsService = Application_Model_NewsService::getInstance();
            $picturesNewService = new Application_Model_PicturesNewService();
            
            $files = $picturesNewService-> findByNewId($delId);
            $newsService->delById($delId);
            if (count($files) > 0){
                foreach ($files as $file){
                    $delPath = Application_Model_PathsService::getInstance()->get("news");
                    if (file_exists($delPath . $file['file_name']))
                        unlink($delPath . $file['file_name']);
                    $delPath = Application_Model_PathsService::getInstance()->get("news", true);
                    if (file_exists($delPath . $file['file_name']))
                        unlink($delPath . $file['file_name']);
                }
            }
        }
        $this->_redirect(   $this
                            ->view
                            ->url(array('controller' => 'index',
                                        'action' => 'index')));
    }

    public function editAction()
    {
        $registry = Zend_Registry::getInstance();
        if (!$registry->acl->isAllowed($registry->currentUser->get('role'), 'edit_news')){
            $this->_redirect(   $this
                            ->view
                            ->url(array('controller' => 'office',
                            'action' => 'index')));
            return;
        }
        $this->_helper->layout->setLayout('layout_work');
        
        $newsService = Application_Model_NewsService::getInstance();
        $formEditNews = new Application_Form_New();
        if ($this->_request->isPost())
        {
            $formData = $this->_request->getPost();
            if ($formEditNews->isValid($formData))
            {
                $formEditNews->processUploadedFiles();
                $values = $formEditNews->getValues();
                $news = $newsService->findById($values['id']);
                $news->set('title', $values['title']);
                $news->set('description', $values['description']);
                $news->set('date_create', time());
                $newId = $newsService->save($news);
                
                $savePath = Application_Model_PathsService::getInstance()->get("news");
                if ($savePath != null && isset($values['photos'])){
                    $picturesNewService = new Application_Model_PicturesNewService();
                    $files = $picturesNewService-> findByNewId($values['id']);
                    foreach ($files as $file){
                        $delPath = Application_Model_PathsService::getInstance()->get("news");
                        if (file_exists($delPath . $file['file_name']))
                            unlink($delPath . $file['file_name']);
                        $delPath = Application_Model_PathsService::getInstance()->get("news", true);
                        if (file_exists($delPath . $file['file_name']))
                            unlink($delPath . $file['file_name']);
                            //var_dump($file['new_id']); die();
                        $picturesNewService-> delById($file['id']);
                    }
                    
                    rename(
                        Application_Model_PathsService::getInstance()->get("temp") . $values['photos'], 
                        $savePath . $values['photos']
                    );
                    
                    $picture_new = new Application_Model_PictureNew();
                    $picture_new -> set('file_name', $values['photos']);
                    $picture_new -> set('new_id', $newId);
                    $pictureNewService = new Application_Model_PicturesNewService();
                    $pictureNewService -> save($picture_new);
                }
                
                $this->_redirect(   $this
                                    ->view
                                    ->url(array('controller' => 'index',
                                                'action' => 'index')));
                return;
            }
        } else {
            $findId = (int)($this->getRequest()->getParam('id'));
            if (isset($findId))
            {
                $res = $newsService->findById($findId);
                if ($res != null)
                {
                    $formEditNews->populate($newsService->mapToArray($res));
                } else {
                     $this->_redirect(   $this
                                ->view
                                ->url(array('controller' => 'index',
                                            'action' =>'index')));
                }
            } else {
                 $this->_redirect(   $this
                                ->view
                                ->url(array('controller' => 'index',
                                            'action' =>'index')));
            }
        }
        
        $this->view->formEditNews = $formEditNews;
    }

}


